package com.nio;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class OracleJDBC {

	public static void main(String[] args) {
		findByPage(3,5);
	}
	
	
	public static void findByPage(int pageIndex,int pageSize){
		Connection conn  =getConnection();
		PreparedStatement stmt = null;
		ResultSet rs  =null;
		try {
			String sql=" select e.* from " +
					"(select rownum r,empno,ename,job,hiredate,deptno from emp) e " + 
					"where r>? and r<?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, (pageIndex-1)*pageSize );  // 第2页  >5    < 11 第3页 >10  <16
			stmt.setInt(2, pageIndex*pageSize+1);
			rs = stmt.executeQuery();
			while(rs.next()){
				int rownum = rs.getInt("r");
				String empno = rs.getString("empno");
				String ename = rs.getString("ename");
				String job = rs.getString("job");
				Date hiredate  =rs.getDate("hiredate");
				int deptno  =rs.getInt("deptno");
				System.out.println(rownum+"\t"+empno+"\t"+ename+"\t"+job+"\t"+hiredate+"\t"+deptno);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	public static Connection getConnection(){
		Connection conn  =null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			String url="jdbc:oracle:thin:@169.254.157.107:1521:orcl";
			String user ="scott";
			String password="tiger";
			conn  =DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

}
